package com.xsky.logic.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.xsky.common.util.Sort;
import com.xsky.logic.service.impl.ZxfaObj;

public class Advice
{
	public String adviceXml ="";
	public static int xh = 0;
	public String supperAdvice(List sourceList)
	{
		String adviceInfo="";
		int seq = 0;
		List rightList = new ArrayList();
		for(int i = 0; i < sourceList.size(); i++)
		{
			List subList = (List)sourceList.get(i);
			for(int j = 0; j < subList.size(); j++)
			{
				ZxfaObj obj = (ZxfaObj)subList.get(j);
				String type = obj.getType();
				if("right".equals(type))
				{
					rightList.add(obj);
					break;
				}
			}
		}
		for(int i = 0; i < sourceList.size(); i++)
		{
			
			List objList = (List)sourceList.get(i);
			for(int j = 0; j < objList.size(); j++)
			{
				seq= seq+1;
				adviceInfo += "<item>";
				adviceInfo+="<xh>"+seq+"</xh>";
				String zxfa = "";
				String yqjg = "";
				ZxfaObj obj = (ZxfaObj)objList.get(j);
				zxfa+=obj.getXh()+":"+obj.getValue()+"\n";
				yqjg+=obj.getQw()+"\n";
				for(int m = 0; m<rightList.size(); m++)
				{
					if(m == i)
					{
						continue;
					}
					ZxfaObj rightObj = (ZxfaObj)rightList.get(m);
					zxfa+=rightObj.getXh()+":"+rightObj.getValue()+"\n";
					yqjg+=rightObj.getQw()+"\n";
				}
				adviceInfo+="<zxfa>"+zxfa+"</zxfa>";
				adviceInfo+="<yqjg>"+yqjg+"</yqjg>";
				adviceInfo+="</item>";
			}
		}
		return adviceInfo;
	}
	public void advice(List sourceList, List flagList, List currList)
	{
		boolean flag = true;
		boolean curFlag = true;
		for(int i = 0; i < currList.size(); i++)
		{
			if((Integer)flagList.get(i)!=(Integer)currList.get(i)+1)
			{
				curFlag = false;
			}
			if((Integer)(currList.get(i))>0)
			{
				flag = false;
			}
		}
		if(curFlag)
		{
			
		}
		else
		{
			if(flag)
			{
				adviceXml+="<item>";
				String zxfa = "";
				String qw = "";
				for(int i = 0; i < sourceList.size(); i++)
				{
					ZxfaObj obj = (ZxfaObj)((List)(sourceList.get(i))).get((Integer)(currList.get(i)));
					zxfa+=obj.getXh()+":"+obj.getValue()+"\n";
					qw+=obj.getQw()+"\n";
				}
				xh = xh+1;
				adviceXml+="<xh>"+xh+"</xh>";
				adviceXml+="<zxfa>"+zxfa+"</zxfa>";
				adviceXml+="<yqjg>"+qw+"</yqjg>";
				adviceXml+="</item>";
				System.out.println("=====================================================");
				System.out.println(adviceXml);
				currList = genCurrList( flagList, currList);
				advice(sourceList, flagList, currList);
			}
			else
			{
				adviceXml+="<item>";
				String zxfa = "";
				String qw = "";
				currList = genCurrList( flagList, currList);
				for(int i = 0; i < sourceList.size(); i++)
				{
					ZxfaObj obj = (ZxfaObj)((List)(sourceList.get(i))).get((Integer)(currList.get(i)));
					//System.out.print(obj.getXh()+":"+obj.getName()+":"+obj.getType()+":"+obj.getValue());
					zxfa+=obj.getXh()+":"+obj.getValue()+"\n";
					qw+=obj.getQw()+"\n";
				}
				xh = xh+1;
				adviceXml+="<xh>"+xh+"</xh>";
				adviceXml+="<zxfa>"+zxfa+"</zxfa>";
				adviceXml+="<yqjg>"+qw+"</yqjg>";
				adviceXml+="</item>";
				System.out.println();
				advice(sourceList, flagList, currList);
			}
		}
	}
	public List suoList(List list)
	{
		Random random = new Random();
		Sort sort = new Sort();
		int[] remove = new int[list.size()-2];
		for(int i = 0; i < list.size()-2; i++)
		{
			remove[i] = Math.abs(random.nextInt())%list.size();
		}
		remove = sort.paopao(remove);
		int last = 0;
		for(int i = 0; i < remove.length; i++)
		{
			if(last==0&&i!=0)
			{
				break;
			}
			if(last==remove[i])
			{
				last = remove[i]-1;
			}
			else
			{
				last = remove[i];
			}
			list.remove(last);
		}
		return list;
	}
	public List genCurrList(List flagList, List currList)
	{
		int[] arr = new int[currList.size()];
		for(int i = 0; i < arr.length; i++)
		{
			arr[i]=(Integer)currList.get(i);
		}
		for(int i=flagList.size()-1; i >=0; i--)//计算到第几位
		{
			int currInt = (Integer)currList.get(i)+1;
			int flagInt = (Integer)flagList.get(i);
			if(currInt<flagInt)
			{
				if(i==flagList.size()-1)
				{
					arr[i]=(Integer)currList.get(i)+1;
				}
				else
				{
					arr[i]=(Integer)currList.get(i)+1;
					for(int j = i+1; j < flagList.size();j++)
					{
						arr[j]=0;
					}
				}
				break;
			}
			else
			{
				
			}
		}
		List resultList = new ArrayList();
		for(int i = 0; i < arr.length; i++)
		{
			resultList.add(arr[i]);
		}
		return resultList;
	}
}
